//
//  BGOHelperUtils.m
//  Bango
//

#import "BGOHelperUtils.h"
#import "BGOAnalyticsConstants.h"

@implementation BGOHelperUtils

+(NSString *)getUTCDate:(NSDate *)localDate
{
	//NSLog(@"> %s", __PRETTY_FUNCTION__);
	//NSLog(@"localDate %@", localDate);
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
	[dateFormatter setTimeZone:timeZone];
	[dateFormatter setDateFormat:BGO_DATETIME_FORMAT];	
	return [dateFormatter stringFromDate:localDate];
}

+(NSString *)getLocalDate:(NSDate *)localDate
{
	//NSLog(@"> %s", __PRETTY_FUNCTION__);
	//NSLog(@"localDate %@", localDate);
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[dateFormatter setDateFormat:BGO_DATETIME_FORMAT];
  return [dateFormatter stringFromDate:localDate];
}


// encode the string -
// this string by mike chambers seems to be better than the apple methods 
// http://mesh.typepad.com/blog/2007/10/url-encoding-wi.html
// simple API that encodes reserved characters according to:
// RFC 3986
// http://tools.ietf.org/html/rfc3986
+(NSString *) urlencode: (NSString *) stringToEncode
{
    NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" ,
							@"@" , @"&" , @"=" , @"+" ,
							@"$" , @"," , @"[" , @"]",
							@"#", @"!", @"'", @"(", 
							@")", @"*", @" ",nil];
	
    NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F" , @"%3F" ,
							 @"%3A" , @"%40" , @"%26" ,
							 @"%3D" , @"%2B" , @"%24" ,
							 @"%2C" , @"%5B" , @"%5D", 
							 @"%23", @"%21", @"%27",
							 @"%28", @"%29", @"%2A", @"%20", nil];
	
    int len = (int)[escapeChars count];
	
    NSMutableString *temp = [stringToEncode mutableCopy];
	
    int i;
    for(i = 0; i < len; i++)
    {
		
        [temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i]
							  withString:[replaceChars objectAtIndex:i]
								 options:NSLiteralSearch
								   range:NSMakeRange(0, [temp length])];
    }
	
	return [temp autorelease];
}

+(double)getRoundedLocation:(double)location {
	NSString *str = [NSString stringWithFormat:@"%.6f", location];
	double result = [str doubleValue];
	return result;
}

+(BOOL)iOSMeetsOrExceedsTargetVersion:(float)targetVersion{
	NSArray *iOSVersion = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
	float floatSystemVersion = [[iOSVersion objectAtIndex:0] floatValue];
	if(iOSVersion.count > 1){
		NSString *subVersion = [iOSVersion objectAtIndex:1];
		if(subVersion.length == 1){
			floatSystemVersion += ([[iOSVersion objectAtIndex:1] floatValue] * 0.01);
		}
		else{
			floatSystemVersion += ([[iOSVersion objectAtIndex:1] floatValue] * 0.10); 
		}
	}
	if(iOSVersion.count > 2){
		NSString *subVersion = [iOSVersion objectAtIndex:2];
		if(subVersion.length == 1){
			floatSystemVersion += ([[iOSVersion objectAtIndex:2] floatValue] * 0.0001);
		}
		else{
			floatSystemVersion += ([[iOSVersion objectAtIndex:2] floatValue] * 0.0010);
		}
	}
	if(floatSystemVersion >= targetVersion){
		return TRUE;		
	}
	else{
		return FALSE;
	}
}

@end
